/*
field 字段查询规则：{name: 'like', sex: '='}  ---  like:模糊, =:精准,range:范围
param 查询字段: {name: 'name', sex: '1'}
page 当前页
pageSize 页码
 */
export const fieldGet = (
    field = {},
    params = {},
    page = '',
    pageSize = ''
) => {
    let filter = {}
    let op = {}
    for(let e in params){
        if(params[e]){
            filter[e] = params[e]
            op[e] = field[e]
        }
    }
    return {
        filter: JSON.stringify(filter),
        op: JSON.stringify(op),
        offset: (page - 1) * pageSize,
        limit: pageSize
    }
}
// 拼接字段 [,]=> ',' / '-'
export const enSplit = (params, field) => {
    let newParams = {...params}
    for(let e in field){
        newParams[e] && (newParams[e] = newParams[e].join(field[e]))
    }
    return newParams
}
// 分割字段 ',' / '-' => [,]
export const exSplit = (params, field) => {
    let newParams = {...params}
    for(let e in field){
        newParams[e] && (newParams[e] = newParams[e].split(field[e]))
    }
    return newParams
}

export const paramChange = (func, form, name, value, idx = null) => {
    let newForm = {...form}
    if(idx !== null){
        let arr = newForm[name] || []
        arr[idx] = value
        newForm[name] = arr
    } else {
        // let idx = value.find(e => {
        //     return e === ''
        // })
        // if(idx > -1){
        //     value = ''
        // }
        newForm[name] = value
    }
    func(newForm)
}

export const useQuery = () => {
    const qs = require('query-string');
    const {useLocation} = require("react-router-dom")
    const {search} = useLocation();
    return qs.parse(search);
}